package com.googlecode.loframework.controller
{
	import com.googlecode.loframework.data.PersonalityQuestionVO;
	import com.googlecode.loframework.model.QuizProxy;
	import com.googlecode.loframework.view.components.quizzes.IChoice;
	import com.googlecode.loframework.view.components.quizzes.IPersonalityQuestion;
	
	import mx.core.UIComponent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	public class QuizItemAddedCommand extends SimpleCommand
	{
		public override function execute(notification:INotification):void
		{
			if(notification.getBody() is IPersonalityQuestion)
			{	
				// get the personality question
				var personalityQuestion:IPersonalityQuestion = notification.getBody() as IPersonalityQuestion;
				
				addPersonalityQuestion(personalityQuestion);
			}
			
		}
		
		protected function addPersonalityQuestion(personalityQuestion:IPersonalityQuestion):void
		{
			// register quiz proxy if one has not already been registered
			if(facade.hasProxy(QuizProxy.NAME) == false)
			{
				facade.registerProxy(new QuizProxy());
			}
			
			// get the quiz proxy
			var proxy:IProxy = facade.retrieveProxy(QuizProxy.NAME);
			
			// create a VO to store data about this personality question
			var vo:PersonalityQuestionVO = new PersonalityQuestionVO();
			vo.groupName = personalityQuestion.quizID;
			vo.value = null;
			
			// get list of choices
			var choices:Array = personalityQuestion.choices;
			
			for(var i:uint=0; i<choices.length; i++)
			{
				vo.characteristics.push((choices[i] as IChoice).value);
			}
			
			
			// add question to the model
			(proxy as QuizProxy).setQuestion(personalityQuestion as UIComponent, vo);
		}
	}
}